package io.renren.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;

import io.renren.entity.SysRetailEntity;
import io.renren.service.SysRetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-14 08:49:21
 */
@RestController
@RequestMapping("sys/sysretail")
@Api(tags = "门店")
public class SysRetailController {
    @Autowired
    private SysRetailService sysRetailService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysRetailService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据ID查询门店信息")
    public R info(@PathVariable("id") Integer id){
        SysRetailEntity sysRetail = sysRetailService.getById(id);

        return R.ok().put("sysRetail", sysRetail);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加")
    public R save(@RequestBody SysRetailEntity sysRetail){
        sysRetailService.save(sysRetail);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public R update(@RequestBody SysRetailEntity sysRetail){
        ValidatorUtils.validateEntity(sysRetail);
        sysRetailService.updateById(sysRetail);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        sysRetailService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 门店进货
     */
    // TODO: 2020/11/17

    /**
     * 库存查看
     */
    @GetMapping("/inventories")
    @ApiOperation("库存查看")
    public R inventories(Integer retailId, @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = sysRetailService.inventories(retailId, params);

        return R.ok().put("page", pageUtils);
    }

    /**
     * 确认收货
     */
    // TODO: 2020/11/17

    /**
     * 根据商品ID和下级门店查询门店
     */
    @GetMapping("/queryByProductAndSubRetail")
    @ApiOperation("根据商品ID和下级门店查询门店")
    public R queryByProductAndSubRetail(
        @RequestParam Integer productId,
        @RequestParam Integer level,
        @RequestParam(required = false)Map<String, Object> params) {

        PageUtils pageUtils = sysRetailService.queryByProductAndSubRetail(productId, level, params);
        return R.ok().put("page", pageUtils);
    }
}
